<template>
  <div class="container">
    <a-card class="general-card" title="URL列表">
      <a-row>
        <a-col :flex="1">
          <a-form
            :model="queryForm"
            :label-col-props="{ span: 6 }"
            :wrapper-col-props="{ span: 18 }"
            label-align="left"
          >
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item field="originUrl" label="原始地址">
                  <a-input
                    v-model="queryForm.originUrl"
                    placeholder="请输入原始地址"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item field="shortId" label="短链ID">
                  <a-input
                    v-model="queryForm.shortId"
                    placeholder="请输入短链ID"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item field="disable" label="状态">
                  <a-select
                    v-model="queryForm.disable"
                    placeholder="请选择状态"
                    allow-clear
                  >
                    <a-option
                      v-for="item of disabledOptions"
                      :key="item.key"
                      :value="item.key"
                      :label="item.label"
                    />
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
        <a-divider style="height: 84px" direction="vertical" />
        <a-col :flex="'86px'" style="text-align: right">
          <a-space direction="vertical" :size="18">
            <a-button type="primary" @click="search">
              <template #icon>
                <icon-search />
              </template>
              查询
            </a-button>
            <a-button @click="reset">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
          </a-space>
        </a-col>
      </a-row>
      <a-divider style="margin-top: 0" />
      <a-row style="margin-bottom: 16px">
        <a-col :span="12">
          <a-space>
            <a-button type="primary" @click="createDs">
              <template #icon>
                <icon-plus />
              </template>
              新建
            </a-button>
          </a-space>
        </a-col>
      </a-row>
      <a-table
        row-key="id"
        :loading="loading"
        :pagination="pagination"
        :columns="columns"
        :data="renderData"
        :bordered="false"
        :size="size"
        @page-change="onPageChange"
        @page-size-change="onPageSizeChange"
      >
        <template #index="{ rowIndex }">
          {{ rowIndex + 1 + (pagination.pageNum - 1) * pagination.pageSize }}
        </template>
        <template #disableType="{ record }">
          <span v-if="record.disable === '0'">正常</span>
          <span v-else-if="record.disable === '1'">禁用</span>
          <span v-else>-</span>
        </template>
        <template #times="{ record }">
          <span v-if="record.times === -1">-</span>
          <span v-else>{{ record.times }}</span>
        </template>
        <template #remainTimes="{ record }">
          <span v-if="record.remainTimes === -1">-</span>
          <span v-else>{{ record.remainTimes }}</span>
        </template>
        <template #operations="{ record }">
          <a-button
            type="text"
            size="small"
            status="success"
            @click="viewDs(record)"
          >
            查看
          </a-button>
          <a-button type="text" size="small" @click="updateDs(record)">
            修改
          </a-button>
          <a-button
            type="text"
            size="small"
            @click="urlRecordChangeStatus(record)"
          >
            <span v-if="record.disable === '0'"> 禁用 </span>
            <span v-else>激活</span>
          </a-button>
          <!-- <a-popconfirm content="确定删除该记录?" @ok="deleteDs(record)">
            <a-button type="text" size="small" status="danger"> 删除 </a-button>
          </a-popconfirm> -->
        </template>
      </a-table>
    </a-card>
    <div class="config-drawer">
      <a-drawer
        width="600px"
        :visible="drawerVisible"
        @ok="submitDatasource"
        @cancel="closeDrawer"
        :mask-closable="false"
        :esc-to-close="false"
        :closable="false"
        :ok-text="drawerOkText"
      >
        <template #title> 短链接 </template>
        <div>
          <a-form :model="formModel">
            <a-form-item field="originUrl" label="原始地址">
              <a-input
                v-model="formModel.originUrl"
                :disabled="viewModel"
                placeholder="请输入原始地址"
              />
            </a-form-item>
            <a-form-item field="shortUrl" label="短链地址">
              <a-input
                v-model="formModel.shortUrl"
                placeholder="请输入短链地址"
                :disabled="viewModel || edit"
              />
            </a-form-item>
            <a-form-item field="shortId" label="短链Id">
              <a-input
                v-model="formModel.shortId"
                placeholder="请输入短链Id"
                :disabled="viewModel || edit"
              />
            </a-form-item>

            <a-form-item
              field="updateTimes"
              label="更新剩余次数"
              v-show="!viewModel && !!formModel.id"
            >
              <a-select v-model="formModel.updateTimes" placeholder="请选择">
                <a-option key="0" value="0" label="否" />
                <a-option key="1" value="1" label="是" />
              </a-select>
            </a-form-item>

            <a-form-item field="times" label="可访问次数">
              <a-input-number
                v-model="formModel.times"
                :disabled="viewModel || formModel.updateTimes !== '1'"
                placeholder="请输入可访问次数"
              />
            </a-form-item>
            <a-form-item field="remainTimes" label="剩余次数">
              <a-input-number
                v-model="formModel.remainTimes"
                :disabled="viewModel || edit"
                placeholder="剩余次数"
              />
            </a-form-item>
            <a-form-item field="expireTime" label="过期时间">
              <a-date-picker
                showTime
                :defaultValue="formModel.expireTime"
                v-model="formModel.expireTime"
                @change="expireTimeChange"
                :disabled="viewModel"
                style="width: 100%"
                placeholder="请选择过期时间"
              />
            </a-form-item>
            <a-form-item field="disable" label="禁用">
              <a-select
                v-model="formModel.disable"
                placeholder="请选择"
                allow-clear
                :disabled="true"
              >
                <a-option key="0" value="0" label="否" />
                <a-option key="1" value="1" label="是" />
              </a-select>
            </a-form-item>
            <a-form-item field="createTime" label="创建时间">
              <a-input
                v-model="formModel.createTime"
                :disabled="viewModel || edit"
                placeholder="创建时间"
              />
            </a-form-item>
            <a-form-item field="createId" label="创建Id">
              <a-input-number
                v-model="formModel.createId"
                :disabled="viewModel || edit"
                placeholder="创建Id"
              />
            </a-form-item>
            <a-form-item field="updateTime" label="更新时间">
              <a-input
                v-model="formModel.updateTime"
                :disabled="viewModel || edit"
                placeholder="更新时间"
              />
            </a-form-item>
            <a-form-item field="updateId" label="更新Id">
              <a-input-number
                v-model="formModel.updateId"
                :disabled="viewModel || edit"
                placeholder="更新Id"
              />
            </a-form-item>
          </a-form>
        </div>
      </a-drawer>
    </div>
  </div>
</template>

<script setup name="datasource">
import { ref, onMounted, watch } from 'vue'
import { IconSearch, IconRefresh, IconPlus } from '@arco-design/web-vue/es/icon'
import { tableColumns } from './table-columns.js'
import {
  getUrlRecorcdList,
  addUrlRecorcd,
  updateUrlRecorcd,
  disabledOrActive
} from '@/api/url-record'
import { Message } from '@arco-design/web-vue'

// 查询表单数据
const queryForm = ref({})
// 分页
const pagination = ref({
  pageNum: 1,
  pageSize: 10,
  total: 1
})
const drawerVisible = ref(false)
const formModel = ref({})
const file = ref()
// 表格列
const columns = ref(tableColumns)
// 后台返回的数据
const renderData = ref([])
const loading = ref(false)
const size = ref('medium')
const edit = ref(false)
const disabledOptions = ref([
  {
    key: '1',
    label: '禁用'
  },
  {
    key: '0',
    label: '正常'
  }
])

const drawerOkText = ref('确定')

// 查看模式
const viewModel = ref(false)

watch(viewModel, (newValue) => {
  if (newValue) {
    drawerOkText.value = '编辑'
  } else {
    drawerOkText.value = '确定'
  }
})

onMounted(() => {
  getDsList()
})

// 查询后台列表数据
const getDsList = () => {
  loading.value = true
  getUrlRecorcdList({ ...queryForm.value, ...pagination.value })
    .then((res) => {
      renderData.value = res.data.records
      pagination.value.total = res.data.total
    })
    .finally(() => {
      loading.value = false
    })
}

const search = () => {
  getDsList()
}
const reset = () => {
  queryForm.value = {}
  pagination.value = {
    pageNum: 1,
    pageSize: 10
  }
  getDsList()
}

const onPageChange = (page) => {
  pagination.value.pageNum = page
  getDsList()
}
const onPageSizeChange = (pageSize) => {
  pagination.value.pageSize = pageSize
  getDsList()
}

const viewDs = (record) => {
  viewModel.value = true
  drawerOkText.value = '编辑'
  drawerVisible.value = true
  formModel.value = { ...record }
}
const updateDs = (record) => {
  viewModel.value = false
  edit.value = true
  drawerOkText.value = '确定'
  drawerVisible.value = true
  formModel.value = { ...record }
  formModel.value.updateTimes = '0'
}
const createDs = () => {
  edit.value = true
  viewModel.value = false
  drawerOkText.value = '确定'
  drawerVisible.value = true
  formModel.value = {
    multiType: '0'
  }
}

const closeDrawer = () => {
  drawerVisible.value = false
  viewModel.value = false
  formModel.value = {}
}

const submitDatasource = () => {
  if (viewModel.value) {
    viewModel.value = false
    return
  }
  const form = { ...formModel.value }
  if (form.id) {
    // 更新
    if (form.updateTimes === '1') {
      form.updateTimes = true
    } else {
      form.updateTimes = false
    }
    updateUrlRecorcd(form).then((res) => {
      Message.success(res.msg)
      drawerVisible.value = false
      getDsList()
      closeDrawer()
    })
  } else {
    // 新增
    addUrlRecorcd(form).then((res) => {
      Message.success(res.msg)
      drawerVisible.value = false
      getDsList()
      closeDrawer()
    })
  }
}

const onLogoChange = (_, currentFile) => {
  file.value = {
    ...currentFile
    // url: URL.createObjectURL(currentFile.file),
  }
}
const expireTimeChange = (dateString, date) => {
  formModel.value.expireTime = dateString
}

const urlRecordChangeStatus = (record) => {
  disabledOrActive(record.id).then((res) => {
    Message.success(res.msg)
    getDsList()
  })
}
</script>

<style lang="less" scoped>
.container {
  padding: 10px;
  background-color: white;
  height: 100%;
}
</style>
